using System;
using System.Windows.Data;
using System.Windows.Media;

namespace ibba.src.sl.recursos.Converters
{
    public class DateTimeConverter : IValueConverter
    {
        private string _label;

        #region Constructions

        public DateTimeConverter()
        {
            _label = "";
        }

        public DateTimeConverter(string pLabel)
        {
            _label = pLabel;
        }

        #endregion

        // This converts the DateTime object to the string to display.
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string data = string.Empty;

            if (value != null)
                data = System.Convert.ToDateTime(value).ToShortDateString();

            if (parameter != null)
            {
                switch (parameter.ToString().ToLower())
                {
                    case "exibirhoje":
                        if (data.Equals(DateTime.Today.ToShortDateString()))
                            return String.Concat(_label, "Hoje");
                        else
                            break;

                    case "destaquehoje":
                        if (data.Equals(DateTime.Today.ToShortDateString()))
                            return new SolidColorBrush(Colors.Red);
                        else
                            return new SolidColorBrush(Colors.Black);

                    //break;

                    default:
                        break;
                }
            }

            if (_label.Length > 0)
                return String.Concat(_label, data); //.ToString("dd/MM/yyyy")));
            else
                return (data.Equals(string.Empty) ? null : data); //.ToString("dd/MM/yyyy"));

        }


        // No need to implement converting back on a one-way binding 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //if (value != null && parameter != null)
            //{
            //    switch (parameter.ToString().ToLower())
            //    {
            //        case "destaquehoje":
            //            if (((DateTime)value).Equals(DateTime.Today))
            //                return new SolidColorBrush(Colors.Red);
            //            else
            //                return new SolidColorBrush(Colors.Black);

            //        default:
            //            break;
            //    }
            //}

            //string strValue = value.ToString(); 
            //DateTime resultDateTime; 
            
            //if (DateTime.TryParse(strValue, out resultDateTime)) 
            //{ 
            //    return resultDateTime; 
            //} 
            return value;
        }

    }
}
